package com.huifer.wgx.controller;

import com.huifer.wgx.model.WorkEntity;
import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.service.WorkService;
import com.huifer.wgx.utlis.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : WorkController </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@Api(tags = "WorkController", description = "工程管理")
@RequestMapping("work")
public class WorkController {

    @Autowired
    private WorkService workService;

    private CommonResult commonResult = new CommonResult();


    @ApiOperation("创建工程")
    @PostMapping("/save")
    public ResponseEntity saveWork(
            @RequestParam("project_name") String projectName,
            @RequestParam("work_name") String workName,
            @RequestParam("start_time") String startTime,
            @RequestParam("personnel_name") String personnelName) {
        try {

            WorkEntity workEntity = workService
                    .saveWork(projectName, workName,
                            DateUtil.getTimestamp(DateUtil.getDateymd(startTime)), personnelName);
            if (workEntity == null) {
                return ResponseEntity.badRequest().body(commonResult.fialed("工程创建失败"));
            } else {
                return ResponseEntity.ok(commonResult.success(workEntity));
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("修改工程(特指工程结束)")
    @PutMapping("/eidt_end")
    public ResponseEntity updateWork(
            @RequestParam("work_name") String workName,
            @RequestParam("end_work") String endWork) {
        try {
            WorkEntity workEntity = workService
                    .updateWork(workName, DateUtil.getTimestamp(DateUtil.getDateymd(endWork)));
            return ResponseEntity.ok(commonResult.success(workEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("修改工程(修改工程名称)")
    @PutMapping("/eidt_work_id")
    public ResponseEntity updateWorkNameById(
            @RequestParam("work_id") Long workId,
            @RequestParam("new_work_name") String newWorkName) {
        try {
            WorkEntity workEntity = workService
                    .updateWork(workId, newWorkName);
            return ResponseEntity.ok(commonResult.success(workEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("修改工程(修改工程名称)")
    @PutMapping("/eidt_work_name")
    public ResponseEntity updateWorkNameById(
            @RequestParam("old_work_name") String oldWorkName,
            @RequestParam("new_work_name") String newWorkName) {
        try {
            WorkEntity workEntity = workService
                    .updateWork(oldWorkName, newWorkName);
            return ResponseEntity.ok(commonResult.success(workEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("查询所有工程")
    @GetMapping("/find_all")
    public ResponseEntity findAllWork(
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity.ok(commonResult.success(workService.findAll(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("根据项目id查询工程")
    @GetMapping("/find_by_id")
    public ResponseEntity findWorkById(
            @RequestParam("project_id") Long projectId,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            Page<WorkEntity> workEntityList = workService
                    .findWorkByProjectId(projectId, pageNum, pageSize);
            return ResponseEntity.ok(commonResult.success(workEntityList));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("根据项目名称查询工程")
    @GetMapping("/find_by_name")
    public ResponseEntity findWorkByName(
            @RequestParam("project_name") String projectName,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(
                            workService.findWorkByProjectName(projectName, pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("根据负责人名称查询工程")
    @GetMapping("/find_by_pname")
    public ResponseEntity findWorkByPname(
            @RequestParam("p_name") String pName,
            @RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            Page<WorkEntity> workEntityList = workService
                    .findWorkByPersonnel(pName, pageNum, pageSize);
            return ResponseEntity.ok(commonResult.success(workEntityList));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }


    @ApiOperation("删除工程根据工程id")
    @DeleteMapping("/remove_by_id")
    public ResponseEntity deleteWorkById(@RequestParam("work_id") Long workId) {
        try {
            WorkEntity workEntity = workService.deleteWork(workId);
            return ResponseEntity.ok(commonResult.success(workEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("删除工程根据工程名称")
    @DeleteMapping("/remove_by_name")
    public ResponseEntity deleteWorkByIdWorkName(@RequestParam("work_name") String workName) {
        try {
            WorkEntity workEntity = workService.deleteWork(workName);
            return ResponseEntity.ok(commonResult.success(workEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }
}
